import java.io.IOException;
import java.io.InputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;


public class Server {
	
	private final static int PORT = 1235;
	private AudioFormat format;
    private int frames;         // total number of frames
    private int frameSize;      // bytes per frame (1 for 8-bit, 2 for 16-bit)
    private int bytes;          // total number of bytes
    private float frameRate;    // frames per second (44100 for CD audio)
    private double seconds;     // duration in seconds
    byte[] data;
    private InetAddress myIpAddress = null;
	private String remoteIpAdress = null;
	private InetAddress remoteAdress = null;
	private DatagramSocket mySendSocket = null;
	private DatagramSocket myRecvSocket = null;
	
    
    public byte[] recv() {		
		
		byte[] buffer = new byte[2000];
		DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
		
		try {
			
			//System.out.println("Server is running with IP: "+this.myIpAddress.getHostAddress().toString());
			
			this.myRecvSocket.receive(packet);			
				
			System.out.println("Getting Packet");
			
			return packet.getData();
			
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null ;
	}
    
    public void setAudiData(byte[] data) {
    	
    	  this.data = data;
          // 44100Hz, 8 bit, 1 channel, PCM signed, little endian
          format = new AudioFormat(8000, 16, 1, true, false);
          frames    = data.length;
          frameSize = 2;   // 8-bit
          frameRate = 8000;
          bytes     = data.length;
          seconds   = frames / frameRate;
          
          System.out.println("Number of seconds: "+seconds);
    }
    
    public void playAudioData() {
    	
    	SourceDataLine line = null;
    	  
        DataLine.Info info  = new DataLine.Info(SourceDataLine.class, format);
        try {
           line = (SourceDataLine) AudioSystem.getLine(info);
           line.open(format);
        }        
       
        
        catch (Exception e) { e.printStackTrace();  System.exit(1);  }
            
        line.start();
        line.write(data, 0, data.length);
        line.drain();
        line.flush();
        line.close();
    }
    
    
	public Server() {
		
		try {
			this.myIpAddress = InetAddress.getLocalHost();
			this.myRecvSocket = new DatagramSocket(PORT);
			this.mySendSocket = this.myRecvSocket; 
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void run() {
		
		System.out.println("Server is running");
		
		while(true) {
			
			byte[] data = this.recv();
			this.setAudiData(data);
			this.playAudioData();
		}
		
		
	}
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		
		//server.run();
		
	/*	byte[] data = new byte[44100];
        double Hz = 659.25;   // E
        for (int i = 0; i < 44100; i++)
            data[i] = (byte) (100 * Math.sin(2 * Math.PI * i * Hz / 44100));*/
		
        Server server = new Server();
       
        server.run();       
                
        
	}

}
